# coding: utf-8
import cv2
import numpy as np
import matplotlib.pyplot as plt

img_BGR = cv2.imread(r"F:\GITGIT\learnCV_homework\week1\week1_homework.png")
img_RGB = cv2.cvtColor(img_BGR, cv2.COLOR_BGR2RGB)
img_roi = img_RGB[100:1780, 190:1340]
#自动高斯核
img_blur_auto = cv2.GaussianBlur(img_roi, (19, 19), 0, 0)  # 高斯模糊

#手工高斯核
#kernel = np.ones((5, 5), np.float32) / 115
#kernel = -kernel
#kernel[0, :] = [2, 4, 5, 4, 2]
#kernel[1, :] = [4, 9, 12, 9, 4]
#kernel[2, :] = [5, 12, 15, 12, 15]
#kernel[2, :] = [4, 9, 12, 9, 4]
#kernel[2, :] = [2, 4, 5, 4, 2]

#img_blur_handmade = cv2.filter2D(img_roi, -1, kernel)

i = 0
while i < 8:
    img_blur_auto = cv2.GaussianBlur(img_blur_auto, (5, 5), 0, 0)
    i += 1

#锐化
kernel = np.array([[0 ,0 ,-1,0,0],
                   [0 ,-1,-1,-1,0],
                   [-1,-1,13.18,-1,-1],
                   [0,-1,-1,-1,0],
                   [0 ,0,-1,0, 0]],np.float32)
img_blur_auto_sharpen = cv2.filter2D(img_blur_auto, -1, kernel)

# 加水印，保护版权
wm = cv2.imread("watermask.png")
wm = 255 - wm
#img_blur_auto_sharpen_watermask = cv2.add(wm, img_blur_auto_sharpen)

img_blur_auto_sharpen_watermask = cv2.addWeighted(wm, 0.7, img_blur_auto_sharpen, 1, 0)
#存储最终结果
final_image = cv2.cvtColor(img_blur_auto_sharpen_watermask, cv2.COLOR_RGB2BGR)
cv2.imwrite("final_image.png", final_image)


#展示过程
plt.figure()
plt.subplot(2,2,1)
plt.title('Cut image',fontsize=12,color='r')
plt.imshow(img_roi)
plt.subplot(2,2,2)
plt.title('Cut_Blur image',fontsize=12,color='r')
plt.imshow(img_blur_auto)
plt.subplot(2,2,3)
plt.title('Cut_Blur_Sharpen image',fontsize=12,color='r')
plt.imshow(img_blur_auto_sharpen)
plt.subplot(2,2,4)
plt.title('Cut_Blur_Sharpen_Watermask image',fontsize=12,color='r')
plt.imshow(img_blur_auto_sharpen_watermask)
plt.show()
